package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.base.util.DateUtil;
import com.epfly.iface.promo.activity.enums.EffectiveDaysEnum;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.impl.ActivityUtil;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 适用星期过滤
 */
@Order(value = PredicateOrder.EFFECTIVE_DAY_ORDER)
@Service
public class EffectiveDayPredicate implements ActivityDiscountPredicate {


    @Override
    public boolean test(ActivityWrapper activityWrapper) {

        ActivityEntity entity = activityWrapper.getActivityEntity();
        // "适用星期"设置为"不限"
        if (entity.getEffectiveDays().equals(ActivityUtil.join(EffectiveDaysEnum.UN_LIMIT.getKey()))) {
            return true;
        }

        Long orderTime = activityWrapper.getOrderTime();
        LocalDateTime localDateTime = LocalDateTime.parse(String.valueOf(orderTime), DateTimeFormatter.ofPattern(DateUtil.COMPACT_TIME_FORMAT));
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        int value = dayOfWeek.getValue();


        // 当前订单时间所属星期属于活动"适用星期"
        return entity.getEffectiveDays().contains(ActivityUtil.join(value));
    }
}
